package com.ithema.reggie.controller;

import com.ithema.reggie.common.R;
import com.ithema.reggie.dto.SetmealDto;
import com.ithema.reggie.entity.Setmeal;
import com.ithema.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.HttpConstraintElement;
import javax.servlet.http.HttpSession;
import java.util.Set;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto, HttpSession session){
        //1.封装setmeal的操作人员信息
        Long userId = (Long) session.getAttribute("employee");
        setmealDto.setCreateUser(userId);
        setmealDto.setUpdateUser(userId);

        //2.调用service完成新增操作
        return setmealService.save(setmealDto);
    }

    /**
     * 作用：展示套餐列表
     *
     * @param pageNum     当前页
     * @param pageSize 页面大小
     * @param name     菜品的名称
     * @return
     */
    @GetMapping("page")
    public R findByPage(@RequestParam("page") Integer pageNum,Integer pageSize, String name){
        return setmealService.findByPage(pageNum,pageSize,name);
    }

    /**
     * 作用：批量删除
     * @param ids  要删除套餐的id
     * @return
     */
    @DeleteMapping
    public R batchDelete(Long[] ids){
        return setmealService.batchDelete(ids);
    }

    @GetMapping("{id}")
    public R findById(@PathVariable Long id){
        return setmealService.findById(id);
    }

    /**
     *作用：修改菜品
     *
     */
    @PutMapping
    public R setmealUpdate(@RequestBody SetmealDto setmealDto,HttpSession session){

        //1.获取当前登陆人id
        Long userId = (Long) session.getAttribute("employee");

        //2.给employee设置更新人id
        setmealDto.setUpdateUser(userId);

        //3.调用service完成更新操作
        return setmealService.update(setmealDto);
    }

    /**
     * 根据套餐的类别展示套餐
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("list")
    public R findOnSaleByCategoruId(Long categoryId,Integer status){
        return setmealService.findOnSaleByCategoruId(categoryId,status);
    }
}
